﻿
Partial Class pages_products_ProductFiles : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Product = CProduct.Cache.GetById(ProductId)
            If IsNothing(Product) Then CSitemap.RecordNotFound("Product", ProductId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductFiles(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Product
            Dim files As CFileList = .Files
            If .IsSet Then files = CFile.Cache.GetByProductId(Me.ProductId)

            UnbindSideMenu()
            AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
            AddMenuSide(Product.TypeName & " Details", CSitemap.ProductEdit(ProductId))
            AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
            If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
            If .CanHaveTranscript Then
                AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
            End If
            AddMenuSide("Vimeo Link" & IIf(.HasVideosOnVimeo, "*", String.Empty) & IIf(.HasVideosOnYouTube, "*", String.Empty), CSitemap.ProductVimeo(ProductId))
            If .CanHaveFiles Then
                AddMenuSide(CUtilities.NameAndCount("Dropbox Files", files), CSitemap.ProductFiles(ProductId), True)
            Else
                AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", files), CSitemap.ProductFiles(ProductId), True)
            End If
            If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
            End If
            If .HasParent Then AddMenuSide("Back to Set...", CSitemap.ProductContents(.ProductBelongsToProductId))

            ctrlFiles.Display(files, False)

            If files.HasHtm OrElse files.HasPdfOrWord Then
                plhNotesAsText.Visible = True
                txtNotesAsText.Enabled = False
                txtNotesAsText.Text = .ProductNotesAsPlainText

            End If
        End With

    End Sub
#End Region


    Protected Sub btnEdit_Click(sender As Object, e As System.EventArgs) Handles btnEdit.Click
        txtNotesAsText.Mode = EControlMode.Editable
        btnEdit.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
    End Sub
    Protected Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        Refresh()
    End Sub
    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        Product.ProductNotesAsPlainText = CUtilities.ReplaceAll(txtNotesAsText.Text, "  ", " ")
        Product.Save()
        Refresh()
    End Sub
End Class
